package info.maxhis.bn.renren;

import info.maxhis.bn.R;

import java.io.File;
import java.util.List;
import java.util.Map;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;

public class ImageAdapter extends BaseAdapter {

    private List<Map<String, String>> datas;

    private RenrenFriendListActivity context;

    ImageAdapter(RenrenFriendListActivity context, List<Map<String, String>> datas) {
        this.context = context;
        this.datas = datas;
    }

    @Override
    public int getCount() {
        return this.datas.size();
    }

    @Override
    public Object getItem(int position) {
        return this.datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder ;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.renren_friend_list_item, null);
            viewHolder = new ViewHolder();
            viewHolder.image = (ImageView) convertView.findViewById(R.id.profile_img);
            viewHolder.name = (TextView) convertView.findViewById(R.id.name);
            viewHolder.birthday = (TextView) convertView.findViewById(R.id.date);
            viewHolder.uid = (TextView) convertView.findViewById(R.id.uid);
            //记录当前位置，不能用参数position，当滚动时会改变
            viewHolder.pos = (TextView) convertView.findViewById(R.id.pos);
            
            viewHolder.checkBox = (CheckBox) convertView.findViewById(R.id.select);
            
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        Map<String, String> friend = this.datas.get(position);
        viewHolder.name.setText(friend.get("name"));
        viewHolder.birthday.setText(friend.get("birthday"));
        viewHolder.pos.setText(friend.get("index"));
        //设置选中状态
        int pos = Integer.parseInt(friend.get("index"));
        viewHolder.checkBox.setChecked(context.selected.get(pos));
        viewHolder.checkBox.setOnClickListener(new OnClickListener() {
        	
        	@Override
        	public void onClick(View v) {
        		View pv = (View) v.getParent();
        		TextView posView = (TextView) pv.findViewById(R.id.pos);
        		int pos = Integer.parseInt(posView.getText().toString());
        		context.selected.set(pos, ((CheckBox)v).isChecked());
        	}
        });
        if (context.share.downloadRenrenHead()) {
        	String dir = Environment.getExternalStorageDirectory()+"/birthday/head";
        	File file = new File(dir, friend.get("uid") + ".jpg");
        	Bitmap bm;
        	if (file.exists()) {
        		bm = BitmapFactory.decodeFile(file.getAbsolutePath());
			} else {
				bm = BitmapFactory.decodeResource(context.getResources(), R.drawable.profile_default);
			}
        	viewHolder.image.setImageBitmap(bm);
		}
        return convertView;
    }

    static class ViewHolder {

        ImageView image;
        TextView name;
        TextView birthday;
        TextView uid;
        TextView pos;
        CheckBox checkBox;
        
    }
    
    /**
     * 复选框监听器
     * @author iStar,2011-7-3
     *
     */
    class CheckedChangeListener implements OnCheckedChangeListener {
    	private int pos;	//复选框的位置
    	
    	public CheckedChangeListener(int pos) {
    		this.pos = pos;
		}
    	
		@Override
		public void onCheckedChanged(CompoundButton buttonView,
				boolean isChecked) {
			context.selected.set(pos, isChecked);
		}
    }
}

